﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using IceLiteBusinessLogic.POCO;

namespace IceLiteBusinessLogic
{
    public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : class 
    {
        private readonly DbContext _context;

        private readonly DbSet<TEntity> _dbSet;

        protected Repository(IUnitOfWork unitOfWork)
        {
            _context = unitOfWork.IceLiteDbContext;
            _dbSet = DbSet;
        }

        protected DbSet<TEntity> DbSet
        {
            get
            {
                return _context.Set<TEntity>();
            }
        }

        public virtual  IQueryable<TEntity> GetAll()
        {
             return _dbSet; 
        }

        public virtual IQueryable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate)
        {
            return DbSet.Where(predicate);
        }

        public virtual void Add(TEntity entity)
        {
            DbSet.Add(entity);
        }

        public virtual void Delete(TEntity entity)
        {
            DbSet.Remove(entity);
        }

        public virtual void Update(TEntity entity)
        {
            _context.Entry(entity).State = EntityState.Modified;
        }

    }


    public class UserRepository: Repository<User>, IUserRepository
    {
        public UserRepository(IUnitOfWork work): base(work)
        {
            
        }
    }

}
